﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using TeamTask.Model;
using System.Data.Objects;
using System.ComponentModel;

namespace TeamTask.Service
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class TeamTaskService
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [WebGet(UriTemplate = "Tasks?skip={skip}&top={top}&owner={userName}")]
        public List<Task> GetTasks(int skip, int top, string userName)
        {
            using (TeamTaskObjectContext objectContext =
                     new TeamTaskObjectContext())
            {
                skip = (skip >= 0) ? skip : 0;
                top = (top > 0) ? top : 25;


                // Include the where clause only if a userName was provided 
                IQueryable<Task> taskQuery = (string.IsNullOrWhiteSpace(userName)) ?
                objectContext.Tasks.AsQueryable<Task>() :
                objectContext.Tasks.Where(task => task.OwnerUserName == userName);

                return taskQuery.OrderBy(task => task.Id)
                                    .Skip(skip).Take(top).ToList();  
            }  
        }


        [WebGet(UriTemplate = "Users/{userName}")]
        public User GetUser(string userName)
        {
            using (TeamTaskObjectContext objectContext =
             new TeamTaskObjectContext())
            {
                // The 'Include' method is an ADO.NET Entity Framework feature that
                //   allows us to load the user and his/her tasks in a single query! 
                var user = objectContext.Users.Include("Tasks")
                                   .FirstOrDefault(u => u.UserName == userName);
                return user;
            } 

        }



        [Description("Allows the details of a single task to be updated.")]
        [WebInvoke(UriTemplate = "Tasks/{id}", Method = "PUT")]
        public Task UpdateTask(string id, Task task)
        {
            // The id from the URI determines the id of the task 
            int parsedId = int.Parse(id);
            task.Id = parsedId;

            using (TeamTaskObjectContext objectContext =
                      new TeamTaskObjectContext())
            {
                objectContext.AttachTask(task);  // We’ll need to implement this!
                objectContext.SaveChanges();
                // Because we allow partial updates, we need to refresh from the dB 
                objectContext.Refresh(RefreshMode.StoreWins, task);
            }

            return task;
        } 

    }



}
